package org.jeecg.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.domain.dto.HistoryOrdersDto;
import org.jeecg.domain.po.HistoryOrdersSync;
import org.jeecg.domain.vo.HistoryOrderVO;
import org.jeecg.domain.vo.OrderVO;
import org.jeecg.service.HistoryOrdersSyncService;
import org.jeecg.mapper.HistoryOrdersSyncMapper;
import org.jeecg.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Calendar;
import java.util.List;

/**
* @author 32327
* @description 针对表【history_orders_sync(历史订单完成15天后同步到历史订单同步表中，通过canal同步到历史订单库中；1天后删除（删除条件当天数据和历史订单库中的订单数据数量一致）)】的数据库操作Service实现
* @createDate 2024-12-25 14:33:29
*/
@Service
public class HistoryOrdersSyncServiceImpl extends ServiceImpl<HistoryOrdersSyncMapper, HistoryOrdersSync>
    implements HistoryOrdersSyncService{
    @Autowired
    private HistoryOrdersSyncMapper historyOrdersSyncMapper;
//    查询历史订单（分页）
    @Override
    public ResponseResult findHistory(HistoryOrdersDto dto) {
//        当前凌晨时间00:00
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date midnightDate = calendar.getTime();
        System.out.println(midnightDate);
        dto.setMorningTime(midnightDate);
        // 获取当前日期的凌晨0:00对应的LocalDateTime
        LocalDateTime currentMidnight = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        // 减去365天得到365天前的凌晨0:00对应的LocalDateTime
        LocalDateTime threeHundredSixtyFiveDaysAgoMidnight = currentMidnight.minus(365, ChronoUnit.DAYS);
        // 将LocalDateTime转换为Date类型
        Date date = Date.from(threeHundredSixtyFiveDaysAgoMidnight.atZone(ZoneId.systemDefault()).toInstant());
        System.out.println(date);
        dto.setYearTime(date);
        //        利用分页
        Page<HistoryOrderVO> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        List<HistoryOrderVO> list = historyOrdersSyncMapper.findPage(page,dto);
        page.setRecords(list);
//        page.setTotal(list.size());
        return ResponseResult.okResult(page);
    }
//    历史订单根据id查询历史订单
    @Override
    public ResponseResult findHistoryById(String id) {
        HistoryOrdersSync historyOrdersSync = historyOrdersSyncMapper.selectHistoryById(id);
        System.out.println(historyOrdersSync);
        return ResponseResult.okResult(historyOrdersSync);
    }
}




